package cn.yunyichina.openapi.proxy.filter;

import cn.yunyichina.openapi.proxy.config.HttpServerConfig;
import cn.yunyichina.openapi.proxy.filter.in.InFilterChain;
import cn.yunyichina.openapi.proxy.filter.in.entity.InRequest;
import cn.yunyichina.openapi.proxy.filter.in.entity.InResponse;
import cn.yunyichina.openapi.proxy.filter.in.module.BankCardNoInFilter;
import cn.yunyichina.openapi.proxy.filter.in.module.IdCardNoInFilter;
import cn.yunyichina.openapi.proxy.filter.in.module.MobileInFilter;
import cn.yunyichina.openapi.proxy.filter.out.OutFilterChain;
import cn.yunyichina.openapi.proxy.filter.out.entity.OutRequest;
import cn.yunyichina.openapi.proxy.filter.out.entity.OutResponse;
import cn.yunyichina.openapi.proxy.filter.out.module.BankCardNoOutFilter;
import cn.yunyichina.openapi.proxy.filter.out.module.IdCardNoOutFilter;
import cn.yunyichina.openapi.proxy.filter.out.module.MobileOutFilter;

/**
 * Created by kevin on 2018/3/1.
 */
public class FilterHandler {

    private static InFilterChain inFilterChain;

    private static OutFilterChain outFilterChain;

    /**
     * 初始化，自动装载处理内容的链
     */
    static {
        inFilterChain=new InFilterChain();
        inFilterChain.addFilter(new IdCardNoInFilter());
        inFilterChain.addFilter(new MobileInFilter());
        inFilterChain.addFilter(new BankCardNoInFilter());

        outFilterChain=new OutFilterChain();
        outFilterChain.addFilter(new IdCardNoOutFilter());
        outFilterChain.addFilter(new MobileOutFilter());
        outFilterChain.addFilter(new BankCardNoOutFilter());
    }

    public static String filterRequestJson(String method,String jsonStr){
        InRequest inRequest=new InRequest();
        inRequest.setMethod(method);
        inRequest.setJsonStr(jsonStr);
        InResponse inResponse= inFilterChain.doFilter(inRequest);
        return inResponse.getJsonStr();
    }

    public static String filterResponseJson(String appId,String method,String jsonStr){
        OutRequest outRequest=new OutRequest();
        outRequest.setAppId(appId);
        outRequest.setMethod(method);
        outRequest.setJsonStr(jsonStr);
        OutResponse outResponse=outFilterChain.doFilter(outRequest);
        return outResponse.getJsonStr();
    }

    public static void main(String[] args) throws Exception {
        /**
         * 测试出参脱敏
         */
        String responseStr="{\"result_code\":\"0\",\"pat_mobile\":\"18316801899\",\"bank_account\":\"6217281918001888039\",\"result_message\":\"success\",\"result\":{\"card_type\":\"0\",\"real_name\":\"朱恒\",\"gender\":\"M\",\"openid\":\"2088112714223975\",\"card_no\":\"420704199312271612\",\"card_no\":\"441622198512246013\",\"card_no\":\"44162219851224601X\",\"card_no\":\"440525600625103\",\"card_no\":\"YRP201801191007274952\",\"access_token\":\"medicalB17fa4700b3f843989118b9bc3ad5cX97\"}}";
        String method="yyt.base.bindCard.list.get";
        String appId="yyt48fe49dbe6eb1cee";
        HttpServerConfig.getConfig().initHttpConfig();
        String result=filterResponseJson(appId,method,responseStr);
        System.out.println("脱敏结果:"+result);

        /**
         * 测试入参还原
         */
        result=filterRequestJson(method,result);
        System.out.println("还原结果:"+result);
    }
}
